package com.guobox.movies.usecase;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;
import com.guobox.movies.constant.Constant;
import com.guobox.movies.entity.BgColors;
import com.guobox.movies.entity.FileItem;
import com.guobox.movies.entity.MovieEntity;
import com.guobox.movies.entity.MovieMedia;
import com.guobox.movies.utils.httpsutils.OkhttpsUtils;
import com.guobox.movies.view.activity.MovieSerialActivity;

import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by xuxinlei on 2017/7/13.
 */

public class PlayMoviesModelImp implements PlayMovieModel {
    private Activity activity;
    private Gson gson = new Gson();
    public static final String AUTHORITY = "com.guobox.player.history.provider";
    private static final Uri HISTORY_URI = Uri.parse("content://" + AUTHORITY + "/list");

    private static final String MOVIE_ENTITY = "movieEntity";

    private static final String ACTION_PLAY = "com.guobox.playerplayservice.action.play";
    private static final String SMB_PATH = "smbPath";
    private static final String PLAY_PATH = "playPath";
    private static final String MOVIE_NAME = "movieName";
    private static final String MOVIE_ID = "movieId";
    private static final String FILE_TYPE = "fileType";
    private static final String FILE_SOURCE = "fileSource";
    private static final String IP = "ip";

    private static final String _Movie = "movie";
    private static final String _File = "file";
    private static final String _PHONE = "phone";
    private static final String _NATIVE = "native";

    public PlayMoviesModelImp(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void playMovies(final MovieEntity movieEntity, final String ip) {
//        Intent intent = new Intent();
//        intent.setComponent(new ComponentName("com.guobox.mg.launcher", "com.guobox.mg.launcher.services.PlayService"));
////            intent.putExtra(Constant.SMB_PATH, movieEntity.getFiles().get(0).getSmbpath());
////            intent.putExtra(Constant.PLAY_PATH, movieEntity.getFiles().get(0).getPlaypath());
//
//        intent.putExtra(Constant.PLAY_MOVIE, gson.toJson(movieEntity, MovieEntity.class));
//        intent.putExtra("movieId", movieEntity.getMetadata().getMovieId());
//        intent.putExtra("BDMovieName", movieEntity.getMetadata().getMovieName());
//
//        intent.putExtra()
//        activity.startService(intent);

        new Thread(){
            @Override
            public void run() {
                Response response = null;
                try {
                    response = OkhttpsUtils.getCheckOkHttpClient().newCall(new Request.Builder().url("http://" + ip + "/application/setting/testsate ")
                            .build()).execute();
                    if (response.body().string().contains("teststateok")) {

                        FileItem fileItem = movieEntity.getFiles().get(movieEntity.getHistory()[0]);
                        MovieMedia movieMedia = movieEntity.getMetadata();
                        String movieName = "";
                        String mediaId = "";
                        if (movieMedia != null) {
                            movieName = movieMedia.getMovieName();
                            mediaId = movieMedia.getMovieId();
                        }
                        addHistory(movieEntity);
                        startActionPlay(activity, fileItem.getSmbpath(), fileItem.getPlaypath(), movieName, mediaId, _Movie, _NATIVE, ip);
                    } else {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(activity, "未连接到设备，请检测网络！", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity, "未连接到设备，请检测网络！", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }.start();


    }

    @Override
    public void startFileListActivity(MovieEntity movieEntity, BgColors bgColors) {

        Intent intent = new Intent(activity, MovieSerialActivity.class);
        intent.putExtra(Constant.BG_COLORS, bgColors);
        intent.putExtra(Constant.PLAY_MOVIE, gson.toJson(movieEntity,MovieEntity.class));
        activity.startActivity(intent);

    }

    public static void startActionPlay(Context context, String smbPath, String playPath, String movieName, String movieId, String fileType, String fileSource, String ip) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.guobox.gbrootservice", "com.guobox.gbrootservice.play.service.PlayIntentService"));
        intent.setAction(ACTION_PLAY);
        intent.putExtra(IP, ip);
        intent.putExtra(SMB_PATH, smbPath);
        intent.putExtra(PLAY_PATH, playPath);
        intent.putExtra(MOVIE_NAME, movieName);
        intent.putExtra(MOVIE_ID, movieId);
        intent.putExtra(FILE_TYPE,fileType);
        intent.putExtra(FILE_SOURCE,fileSource);
        context.startService(intent);
    }

    private void addHistory(MovieEntity movieEntity) {
        ContentResolver contentResolver = activity.getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MOVIE_ID,movieEntity.getId());
        Gson gson = new Gson();
        String movieString = "";
        try {
            movieString = gson.toJson(movieEntity, MovieEntity.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(movieString)) {
            contentValues.put(MOVIE_ENTITY,movieString);
            contentResolver.insert(HISTORY_URI, contentValues);
        }
    }
}
